package com.shark;

import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @auther shark
 * @create 2024-08-09 22:39
 */
@SpringBootTest
public class RabbitMQTest4DeadLetter
{
    @Resource
    RabbitTemplate rabbitTemplate;

    public static final String EXCHANGE_NORMAL = "exchange.normal.video";
    public static final String QUEUE_NORMAL = "queue.normal.video";
    public static final String ROUTING_KEY_NORMAL = "routing.key.normal.video";


    public static final String EXCHANGE_DEAD_LETTER = "exchange.dead.letter.video";
    public static final String QUEUE_DEAD_LETTER = "queue.dead.letter.video";
    public static final String ROUTING_KEY_DEAD_LETTER = "routing.key.dead.letter.video";



    @Test
    public void testSendMessageButReject() {
        rabbitTemplate.convertAndSend(
                        EXCHANGE_NORMAL,
                        ROUTING_KEY_NORMAL,
                        "测试死信情况1：消息被拒绝");
    }




    /**
     * 停止消费微服务，先发送15条消息
     *
     * 个数正常是10条，多了5条溢出；
     *
     * 本次设置10秒钟超时，在正常队列里面的10条记录，到时间后也会变成死信，最后该10条也流入死信交换机
     *
     * 溢出5条+超时10秒后，全部15条都走进死信队列后再启动消费微服务
     */
    @Test
    public void testSendMultiMessage() {
        for (int i = 1; i <= 20; i++) {
            rabbitTemplate.convertAndSend(
                    EXCHANGE_NORMAL,
                    ROUTING_KEY_NORMAL,
                    "测试死信情况2：消息数量超过队列的最大容量" + i);
        }
    }




    @Test
    public void testSendMessageTimeout() {
        rabbitTemplate
                .convertAndSend(
                        EXCHANGE_NORMAL,
                        ROUTING_KEY_NORMAL,
                        "测试死信情况3：消息超时");
    }

}
